package com.android.locationapi.maps;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity{

	ProgressDialog myProgressDialog = null;
	
	protected void onCreate(Bundle bundle) {
		setRequestedOrientation(1);
		super.onCreate(bundle);
		
		setContentView(R.layout.registration);

        final Context ctx = this.getApplicationContext();
		final EditText userName = (EditText) findViewById(R.id.userName_TextBox);
		final EditText password = (EditText) findViewById(R.id.password_TextBox);
		final EditText rePassword = (EditText) findViewById(R.id.rePassword_TextBox);
		
		Button registerButton = (Button) findViewById(R.id.RegisterButton);
	    registerButton.setOnClickListener(new View.OnClickListener() {		
	    	@Override
			public void onClick(View v) {

	    		myProgressDialog = ProgressDialog.show(Register.this,"Please wait...", "Creating new user...", true);
		
                final String user_Name = userName.getText().toString();
                final String _password = password.getText().toString();
                final String re_password = rePassword.getText().toString();
                
                Log.v("ramya : value for creating the username :",user_Name);
                Log.v("ramya : value for creating the password :",_password);
                Log.v("ramya : value for creating the password :",re_password);
                
     	        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
       	        nameValuePairs.add(new BasicNameValuePair("username",user_Name));
       	        nameValuePairs.add(new BasicNameValuePair("password",_password));
       	        nameValuePairs.add(new BasicNameValuePair("password2",re_password));
       	        
  	            InputStream is = null;
      	        String result = "";

     	          try{
   	                  HttpClient httpclient = new DefaultHttpClient();
   	                  HttpPost httppost = new HttpPost(serveripaddress.servipaddr+"register.php");
   	                  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
   	                  Log.v("Ramya", "I am done 1!!!");
   	                  HttpResponse response = httpclient.execute(httppost);
   	                  Log.v("Ramya", "I am done 2!!!");
   	                  HttpEntity entity = response.getEntity();
   	               Log.v("Ramya", "here now");
   	                 is = entity.getContent();
   	               Log.v("Ramya: Returned from the server 0 : ", is.toString());

   	          }catch(Exception e){
   	                  Log.v("log_tag", "Error in http connection "+e.toString());
   	          }
   	          //convert response to string
   	          try{
   	                  BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
   	                  StringBuilder sb = new StringBuilder();
   	                  String line = null;
   	                  while ((line = reader.readLine()) != null) {
   	                          sb.append(line);
   	                  }
   	                  is.close();
   	           
   	                  result=sb.toString();
   	                  Log.v("Ramya: Returned from the server : ", result);
   	                  
   	                  if ( result.equals("ok"))
   	                {
   	                	 Toast.makeText(ctx,"Success!!! :: User profile created successfully,Login with your credentials...", 9000).show();
   	                	 Intent explicitIntent5= new Intent(Register.this,creden.class);
                         
                         startActivity(explicitIntent5);
   	                  }
   	                  else  if ( result.equals("null"))
   	                  {
   	                	 Toast.makeText(ctx,"Error!!! :: Both the username and password field should be entered.", 6000).show();
   	                  }
   	                  else if ( result.equals("exists"))
   	                  {
   	                	 Toast.makeText(ctx,"Error!!! :: Duplicate username, try with a different username", 6000).show();
   	                	  
   	                  }
   	                  
   	                
   	                  
   	                  
   	                  
   	          }catch(Exception e){
   	                  Log.v("log_tag", "Error converting result "+e.toString());
   	          }
            	 
                                    
                     
            
             myProgressDialog.dismiss();
  




	    	}
	    });

	   	    		
	    Button cancelButton = (Button) findViewById(R.id.cancelButton);
	    cancelButton.setOnClickListener(new View.OnClickListener() {		
	    	@Override
			public void onClick(View v) {
	    		
	    		Intent myIntent = new Intent(Register.this, creden.class);
	        	Register.this.startActivity(myIntent);  		
	    	}
	    });
	    	
	    
	}	
}
